<template>
	<el-cascader filterable clearable style="margin-right: 10px;width:280px;" v-model="contentValue"
		:show-all-levels="true" :options="optionsValue" :props="{
					value: 'id',
					label: 'name',
					leaf: 'isLeaf',
					emitPath: true,
				}" @change="$emit('change')">
	</el-cascader>
</template>

<script>
	export default {
		components: {

		},
		data() {
			return {}
		},
		props: {
			modelValue: {
				type: String,
				default: ""
			},
			type: {
				type: String,
				default: ""
			},
			options: {
				type: Object,
				default: () => {}
			}
		},
		computed: {
			optionsValue: {
				get() {
					if (this.type == "key") {
						return this.filterNodes(this.options, (node) => {
							return ~node.type.indexOf("primary key")
						});
					};
					if (this.type == "指标") {
						return this.filterNodes(this.options, (node) => {
							if (node.type) {
								return ~node.type.indexOf("count") || ~node.type.indexOf("sum")
							}
						});
					};
					return this.filterNodes(this.options, (node) => {
						return !~node.type.indexOf("count") && !~node.type.indexOf("sum")
					});
				}
			},
			contentValue: {
				get() {
					return this.modelValue
				},
				set(val) {
					this.$emit('update:modelValue', val);
				}
			}
		},
		methods: {
			filterNodes(nodes, cb) {
				return nodes.filter(node => {
					if (node.children) {
						node.children = this.filterNodes(node.children, cb);
						return node.children.length > 0 || cb(node);
					}
					return cb(node); // 只保留目标叶节点
				});
			}
		}
	}
</script>

<style>
</style>
